package org.jeecg.modules.lab.chart.service.impl;

import org.jeecg.modules.lab.chart.mapper.ChartAnlsMapper;
import org.jeecg.modules.lab.chart.service.ChartAnlsService;
import org.jeecg.modules.lab.chart.vo.KyItemsVo;
import org.jeecg.modules.lab.chart.vo.KyRyItemsVo;
import org.jeecg.modules.lab.chart.vo.KyYmItemsVo;
import org.jeecg.modules.lab.items.item.mapper.LabItemsMapper;
import org.jeecg.modules.lab.utensil.entity.MonthList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ChartAnlsServiceImpl implements ChartAnlsService {

    @Autowired
    private ChartAnlsMapper chartAnlsMapper;

    @Override
    public Map<String, Object> kyXmAnalyseData(String userId, String year) {
        Map<String, Object> map = new HashMap<>();

        // 科研项目数据
        List<KyItemsVo> kyItemsListA = chartAnlsMapper.getKyItemsList(year);
        if (kyItemsListA != null && kyItemsListA.size() > 0) {
            Integer numB = 0; // 在进行科研项目数量
            Integer numC = 0; // 已完成科研项目数量
            for (KyItemsVo vo : kyItemsListA) {
                if ("complete".equals(vo.getStatus())) {
                    numC = numC + 1;
                } else {
                    numB = numB + 1;
                }
            }
            map.put("kyNumA", kyItemsListA.size());
            map.put("kyNumB", numB);
            map.put("kyNumC", numC);
        } else {
            map.put("kyNumA", 0);
            map.put("kyNumB", 0);
            map.put("kyNumC", 0);
        }
        // 科研实验数据
        List<KyItemsVo> kyItemsListB = chartAnlsMapper.getXmItemsList(year);
        if (kyItemsListB != null && kyItemsListB.size() > 0) {
            Integer numB = 0; // 在进行科研实验数量
            Integer numC = 0; // 已完成科研实验数量
            for (KyItemsVo vo : kyItemsListA) {
                if ("已实验".equals(vo.getStatus())) {
                    numC = numC + 1;
                } else {
                    numB = numB + 1;
                }
            }
            map.put("xmNumA", kyItemsListA.size());
            map.put("xmNumB", numB);
            map.put("xmNumC", numC);
        } else {
            map.put("xmNumA", 0);
            map.put("xmNumB", 0);
            map.put("xmNumC", 0);
        }
        // 实验项目统计
        // 实验项目统计——查询数据
        String ymMatA = year + "-0";
        String ymMatB = year + "-";
        List<KyYmItemsVo> kyYmItemsVoList = chartAnlsMapper.getYmItemsList(year, ymMatA, ymMatB);

        Map<String, Object> optionXm = new HashMap<>();
        Map<String, Object> xAxis = new HashMap<>(); // x轴
        xAxis.put("type", "category");
        List<String> numMonth = new ArrayList<>();
        if (kyYmItemsVoList != null && kyYmItemsVoList.size() > 0) {
            for (int i = 1; i <= 12; i++) {
                numMonth.add(i + "月");
            }
            xAxis.put("data", numMonth); // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        }else {
            xAxis.put("data", null); // data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        }
        optionXm.put("xAxis", xAxis);
        Map<String, Object> yAxis = new HashMap<>(); // y轴
        yAxis.put("type", "value");
        optionXm.put("yAxis", yAxis);

        Map<String, Object> series = new HashMap<>(); // y轴数据
        series.put("type", "line");

        if (kyYmItemsVoList != null && kyYmItemsVoList.size() > 0) {
            List<String> numValue = new ArrayList<>();
            for (String m : numMonth) {
                String value = null;
                for (KyYmItemsVo vo : kyYmItemsVoList) {
                    if (m.equals(vo.getYm())) {
                        value = String.valueOf(vo.getNum());
                    }
                }
                numValue.add(value);
            }
            series.put("data", numValue); // data: [150, 230, 224, 218, 135, 147, 260],
        } else {

            series.put("data", null);
        }
        optionXm.put("series", series);
        map.put("optionXm", optionXm);

        // 人员工作量统计
        List<KyRyItemsVo> kyRyItemsVoList = chartAnlsMapper.getRyItemsList(year);
        Map<String, Object> optionRy = new HashMap<>();
        Map<String, Object> xAxisRy = new HashMap<>(); // x轴
        xAxisRy.put("type", "category");
        List<String> ryList = new ArrayList<>();
        List<String> ryValue = new ArrayList<>();
        if (kyRyItemsVoList != null && kyRyItemsVoList.size() > 0) {
            for (KyRyItemsVo vo : kyRyItemsVoList) {
                ryList.add(vo.getUserName());
                ryValue.add(String.valueOf(vo.getNum()));
            }
        } else {
            ryList = null;
            ryValue = null;
        }
        xAxisRy.put("data", ryList);// data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        optionRy.put("xAxis", xAxisRy);

        Map<String, Object> yAxisRy = new HashMap<>(); // y轴
        yAxisRy.put("type", "value");
        optionXm.put("yAxis", yAxisRy);
        Map<String, Object> seriesRy = new HashMap<>(); // y轴数据
        seriesRy.put("type", "bar");
        seriesRy.put("data", ryValue); // data: [150, 230, 224, 218, 135, 147, 260],
        optionRy.put("series", seriesRy);

        map.put("optionRy", optionRy);
        return map;
    }
}
